﻿using Microsoft.ReportingServices.ReportProcessing.ReportObjectModel;
using ReportSystem_XY.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ReportSystem_XY
{
    public partial class FrmLogin : Form
    {
        public FrmLogin()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            //接收信息
            string userName = txtUName.Text.Trim();
            string userPwd = txtUPwd.Text.Trim();
            //判断是否为空
            if (string.IsNullOrEmpty(userName))
            {
                MsgBoxHelper.MsgErrorShow("账号不能为空！");
                txtUName.Focus();
                return;
            }
            if (string.IsNullOrEmpty(userPwd))
            {
                MsgBoxHelper.MsgErrorShow("密码不能为空！");
                txtUPwd.Focus();
                return;
            }
            //加密
            string enPwd = MD5Encrypt.Encrypt(userPwd);
            //登录检查

            //Action act = () =>
            //{
            //    string sql = "select a.*,b.RoleName from UserInfos a join RoleName b on a.RoleId=b.RoleId where UserName='" + userName + "' AND UserPwd='" + MD5Encrypt.Encrypt(userPwd) + "'";
            //    List<UserInfos> userinfo = UserInfos.GetUserInfoListAll(sql);
            //    if (userinfo.Count <= 0)
            //    {
            //        MsgBoxHelper.MsgErrorShow("账号或密码输入有误，请检查！");
            //        return;
            //    }
            //    else
            //    {
            //        //转到主页面
            //        if (!FormUtility.CheckOpenForm("FrmMain"))
            //        {
            //            FrmMain fMain = new FrmMain();
            //            //登录页面显示处理---隐藏，不能关闭
            //            fMain.Tag = userinfo[0];
            //            fMain.Show();
            //        }
            //        else
            //        {
            //            //更换登录者时发生（暂留，后面实现）
            //            //FormUtility.ShowOpenForm("FrmMain");
            //            foreach (Form frm in Application.OpenForms)
            //            {
            //                if (frm.Name == "FrmMain")
            //                {
            //                    frm.Tag = userinfo[0];
            //                    frm.Show();
            //                    break;
            //                }

            //            }
            //        }
            //        this.Hide();
            //    }
            //};
            //act.TryCatch("登录系统出现异常");
        }

        private void FrmLogin_FormClosing(object sender, FormClosingEventArgs e)
        {
            //FormUtility.CloseAllForm();
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
            txtUName.Text = string.Empty;
            txtUPwd.Text = string.Empty;
        }

        private void FrmLogin_VisibleChanged(object sender, EventArgs e)
        {
            UserInfos info = this.Tag as UserInfos;
            if (info != null)
            {
                if (info.RoleId == 101)
                {
                    Action act = () =>
                    {
                        txtUName.Text = info.UserName;
                        txtUPwd.Text = info.UserPwd;
                    };
                    act.TryCatch("主页面初始化出现异常");
                }
            }
        }
    }
}